/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.facade;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import jpa.entities.Paciente;

/**
 *
 * @author issao
 */
@Stateless
public class PacienteFacade extends AbstractFacade<Paciente> {
    @PersistenceContext(unitName = "IMOApp-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public PacienteFacade() {
        super(Paciente.class);
    }
    
    public Paciente buscaPaciente(String nome, String rg, String orgaoEmissor, String uf, String ctps, String serieCtps, String ufCtps) {
        String ejbql = "SELECT p FROM paciente p "
                + "WHERE p.nome = :nome "
                + "AND ("
                + " (p.rg=:rg and p.orgaoEmissor=:orgaoEmissor and p.uf=:uf) "
                + " OR (p.ctps=:ctps and p.serieCtps=:serieCtps and p.ufCtps=:ufCtps)"
                + ")";
        Query query = em.createQuery(ejbql);
        query.setParameter("nome", nome);
        query.setParameter("rg", rg);
        query.setParameter("orgaoEmissor", orgaoEmissor);
        query.setParameter("uf", uf);
        query.setParameter("ctps", ctps);
        query.setParameter("serieCtps", serieCtps);
        query.setParameter("ufCtps", ufCtps);
        return (Paciente) query.getSingleResult();
    }
    
}
